package com.chinaoryx.er;

import org.apache.log4j.Logger;
import org.dom4j.*;
import org.dom4j.io.SAXReader;

import java.net.URL;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: hyman.zhang
 * Date: 12-2-10
 * Time:
 */
public class ConfigReader {
    static Logger logger = Logger.getLogger(ConfigReader.class);


    public EntityDefination readEntity(String entityId, String configFile) throws DocumentException {
        EntityDefination entity = null;

        SAXReader reader = new SAXReader();
        Document document = reader.read(configFile);
//        String xpath = "/root/entity[id=entityId]";
//        Node entityNode = document.selectSingleNode(xpath);
        Element rootElement = document.getRootElement();
        List<Element> entityListElement = null;
        entityListElement = rootElement.elements();
        Element entityElement = null;
        for (Element e : entityListElement) {
               Attribute attr = e.attribute("id");
               if (entityId.equals(attr.getValue())) {
                   entityElement = e;
                   break;
               }
        }


//        logger.info(entityElement);
        if (entityElement == null) {
            return null;
        }
        
        entity = new EntityDefination();

        Element pkNameEle = entityElement.element("primaryKeyName");
        String pkName = pkNameEle.getTextTrim();
        TableColumn pkObject = new TableColumn();
        pkObject.setColumnName(pkName);
        entity.setPrimaryKey(pkObject);
        
        
        
        Element tableElement = entityElement.element("priTableDefination");


        TableDefination priTable = null;
        priTable = this.loadTableDefination(null, tableElement);
        entity.setTable(priTable);
        return entity;
    }


    private TableDefination loadTableDefination(TableDefination parentTable, Element curTableElement) {

        TableDefination table = new TableDefination();

        Element tableNameElement = curTableElement.element("tableName");
        Element aliasNameElement = curTableElement.element("tableAliasName");
        Element subTablesElement = curTableElement.element("subTables");
        Element columnMapElement = curTableElement.element("columnMap");
        String tableName = tableNameElement.getTextTrim();
        String aliasName = aliasNameElement.getTextTrim();

        if (columnMapElement != null) {
            String curColumnName = columnMapElement.elementTextTrim("columnName");
            String parentColumnName = columnMapElement.elementTextTrim("parentColumnName");
            TableDefination.ColumnMap cm = new TableDefination.ColumnMap();
            cm.setColumnName(curColumnName);
            cm.setParentColumnName(parentColumnName);
            table.setColumnMap(cm);
        }

        table.setTableName(tableName);
        table.setAliasName(aliasName);
        table.setParentTable(parentTable);
        
        if (subTablesElement != null) {
            List<TableDefination> subTables = new ArrayList<TableDefination>();
            List<Element> subTablesElementList = subTablesElement.elements("tableDefination");
            for (Element element : subTablesElementList) {
                TableDefination subTable = this.loadTableDefination(table, element);
                subTables.add(subTable);
            }
            table.setSubTables(subTables);
        }

        return table;
    }
}
